package com.b2.service.impl;

import com.b2.constant.MessageConstant;
import com.b2.context.BaseContext;
import com.b2.mapper.CourseCategoryMapper;
import com.b2.mapper.CourseMapper;
import com.b2.mapper.WebUserMapper;
import com.b2.pojo.entity.Course;
import com.b2.pojo.entity.CourseCategory;
import com.b2.pojo.entity.WebUser;
import com.b2.result.Result;
import com.b2.service.CourseCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author
* @description 针对表【video_category】的数据库操作Service实现
* @createDate 2024-04-25 16:05:17
*/
@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory>
    implements CourseCategoryService {

    // 注入VideoCategoryMapper的接口对象
    @Resource
    private CourseCategoryMapper courseCategoryMapper;

    @Resource
    private CourseMapper courseMapper;
    @Autowired
    private WebUserMapper webUserMapper;


    /**
     * 查找所有的课程类型
     * @return 返回课程类型
     */
    @Override
    public Result<List<CourseCategory>> check() {
        // 查找所有的课程类型
        Long userId = BaseContext.getCurrentId();
        WebUser webUser = webUserMapper.selectById(userId);
        QueryWrapper<CourseCategory> queryWrapper = new QueryWrapper<>();
        if (webUser.getUserRole().equals(0)) {
            queryWrapper.eq("userId", userId);
        }
        List<CourseCategory> lists = courseCategoryMapper.selectList(queryWrapper);
        return Result.success(lists);
    }

    /**
     * 添加课程类型
     * @param CategoryName 课程类型名
     * @return 返回响应结果
     */
    @Override
    @Transactional
    public Result<List<CourseCategory>> add(String CategoryName) {
        // 判断标记类型名是否为空
        if(CategoryName == null || CategoryName.equals("")) {
            throw new RuntimeException(MessageConstant.COURSE_CATEGORY_EMPTY);
        }
        // 创建LambdaQueryWrapper条件对象
        LambdaQueryWrapper<CourseCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 设置videoCategoryName条件
        lambdaQueryWrapper.eq(CourseCategory::getCategoryName, CategoryName);
        // 通过videoCategoryName查找数据
        CourseCategory originalCategory = courseCategoryMapper.selectOne(lambdaQueryWrapper);
        // 判断数据是否存在
        if (originalCategory != null) {
            throw new RuntimeException(MessageConstant.COURSE_CATEGORY_EXIST);
        }
        Long userId = BaseContext.getCurrentId();

        // 创建VideoCategory对象
        CourseCategory courseCategory = new CourseCategory();
        // 设置视频类型名
        courseCategory.setCategoryName(CategoryName);
        // 设置用户id
        courseCategory.setUserId(userId);
        // 添加视频类型
        courseCategoryMapper.insert(courseCategory);
        // 回显数据
        List<CourseCategory> lists = courseCategoryMapper.selectList(new QueryWrapper<>());
        // 返回响应结果
        return Result.success(lists);
    }

    /**
     * 根据id删除课程类型
     * @param categoryId 课程类型id
     * @return 返回响应结果
     */
    @Override
    @Transactional
    public Result<List<CourseCategory>> delete(int categoryId) {
        // 创建LambdaQueryWrapper条件对象
        LambdaQueryWrapper<CourseCategory> originalQueryWrapper = new LambdaQueryWrapper<>();
        // 设置categoryId条件
        originalQueryWrapper.eq(CourseCategory::getCategoryId, categoryId);
        // 通过categoryId查找数据
        CourseCategory originalCategoryType = courseCategoryMapper.selectOne(originalQueryWrapper);
        // 判断数据是否存在
        if (originalCategoryType == null) {
            throw new RuntimeException(MessageConstant.COURSE_CATEGORY_NOT_EXIST);
        }
        // 创建LambdaQueryWrapper条件对象
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        // 设置CategoryId条件
        queryWrapper.eq(Course::getCategoryId, categoryId);
        // 通过categoryId查找课程资源
        List<Course> courseList = courseMapper.selectList(queryWrapper);
        // 判断数据是否存在
        if (!courseList.isEmpty()) {
            throw new RuntimeException(MessageConstant.NOT_ALLOW_DELETE);
        }

        // 根据id删除课程类型
        courseCategoryMapper.deleteById(categoryId);
        // 回显数据
        List<CourseCategory> lists = courseCategoryMapper.selectList(new QueryWrapper<>());
        // 返回响应结果
        return Result.success(lists);
    }
}




